package com.app.newiptv.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;

/**
 * Created by dell on 2018/2/9.
 */

public class NetChangeService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        registerBroadcastReceiver();
        Log.i("NetReceiver", "registerBroadcastReceiver");
        boolean connect = CheckNetWork();
        Log.i("NetReceiver", "net connect: " + connect);
        if (!connect) {
            Intent mIntent = new Intent();
            mIntent.setAction("iptv.net.conn.CONNECTIVITY_CHANGE");
            sendBroadcast(mIntent);
        }
    }

    private void registerBroadcastReceiver() {
        IntentFilter USBsdcardFilter = new IntentFilter();
        USBsdcardFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        USBsdcardFilter.addAction("iptv.net.conn.CONNECTIVITY_CHANGE");
        NetReceiver netChangeReceiver = new NetReceiver();
        registerReceiver(netChangeReceiver, USBsdcardFilter);
    }

    private boolean CheckNetWork() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null || !info.isAvailable()) {
            return false;
        } else {
            return true;
        }
    }
}
